require.config({
    baseUrl:"./",
    paths:{
        jq: "./libs/jquery",
        gg: "./modules/getGoods1",
        cr: "./modules/cartRender",
        rl: "./modules/resetLocal"
    }
});
require(["jq","gg","cr","rl"],function(_, getGoods1, render, resetLocal){

    const url = "http://localhost:3000/api";
    const tbody = $("tbody");
    let goods = localStorage.getItem("goods") ? JSON.parse(localStorage.getItem("goods")) : [];
    getGoods1(url,res=>{
        // console.log(res);
        res = JSON.parse(res).msg;
        // console.log(res);
        render(goods, res, tbody);

        $("#allNum").html(allNum(goods));

        $("#allPrice").html(allPrice(goods));
    })
    tbody.on("click", ".del", function(){
        const id = $(this).parent().attr("index");
        $(this).parent().remove();
         // console.log(id)
        resetLocal(goods, id, function(i){
            goods.splice(i, 1)
        })
        $("#allNum").html(allNum(goods));
        $("#allPrice").html(allPrice(goods));
        $("#allCheck")[0].checked = allFlag(goods);
    })
//+、-按钮功能
    //1.“-” 的功能

    tbody.on("click",".lose",function(){
        const id = $(this).parent().parent().parent().attr("index");
        // console.log(id);
        // let setNum=$(this).next().val()
        // console.log(setNum);
        // console.log($(this).next().val());
        if($(this).next().val()>1){
            $(this).next("input").val($(this).next("input").val()-1) 
            const next = $(this).next("input").val()
            resetLocal(goods, id, function(i){
                goods[i].num = next-0
            })
            getGoods1(url,res=>{
                // console.log(res);
                res = JSON.parse(res).msg;
                render(goods, res, tbody);
            })
            $("#allNum").html(allNum(goods));
            $("#allPrice").html(allPrice(goods));
            $("#allCheck")[0].checked = allFlag(goods);
        }
    })
//2."+"按钮功能

    tbody.on("click",".add",function(){
        const id = $(this).parent().parent().parent().attr("index");
        // console.log(id);
        $(this).prev("input").val(($(this).prev("input").val()-0)+1 )
        // console.log($(this).prev("input").val());

        const prev = $(this).prev("input").val()
        resetLocal(goods, id, function(i){
            goods[i].num = prev-0
        })
        getGoods1(url,res=>{
            // console.log(res);
            res = JSON.parse(res).msg;
            render(goods, res, tbody);
        })
        $("#allNum").html(allNum(goods));
        $("#allPrice").html(allPrice(goods));
        $("#allCheck")[0].checked = allFlag(goods);
    })
    

    //==========================
    //input 的加减功能




     tbody.on("input", ".setNum", function(){
        const id = $(this).parent().parent().parent().attr("index");
        const num = $(this).val()-0;
    //  console.log(id)
     
        resetLocal(goods, id, function(i){
            goods[i].num = num;
            // console.log(  goods[i].num);        
        })
        // render(goods, res, tbody);

        getGoods1(url,res=>{
            // console.log(res);
            res = JSON.parse(res).msg;
            render(goods, res, tbody);
        })
        // $("#allCheck")[0].checked = allFlag(goods);
        $("#allNum").html(allNum(goods));
        $("#allPrice").html(allPrice(goods));

        
    })

        //==================================






     // 单个选中事件
 tbody.on("change", ".check", function(){
    const id = $(this).parent().parent().attr("index");
    const flag = this.checked;
    // console.log(flag);
    resetLocal(goods, id, function(i){
        goods[i].flag = Number(flag)
    })
    $("#allNum").html(allNum(goods));
    $("#allPrice").html(allPrice(goods));
    $("#allCheck")[0].checked = allFlag(goods);

})

// 全选事件
$("#allCheck").change(function(){

    goods.forEach(val=>{
        val.flag = Number(this.checked);
    })
    localStorage.setItem("goods", JSON.stringify(goods))
    for(var i=0;i<$(".check").length;i++){
        $(".check")[i].checked = this.checked;
    }
    $("#allNum").html(allNum(goods));
    $("#allPrice").html(allPrice(goods));
    $("#allCheck")[0].checked = allFlag(goods);



  })
})
// 计算总数量
function allNum(goods){  
    return goods.reduce((prev, val)=>{
        return val.flag ? prev + val.num : prev;
    }, 0)
}
// 计算总价
function allPrice(goods){
    return goods.reduce((prev, val)=>{
        return val.flag ? prev + val.num*val.price : prev;
    
    }, 0)
}
// 判断是否需要全选
function allFlag(goods){
    return goods.every(val=>val.flag)
}